package com.itqf.studentinfo.controller;

import com.github.pagehelper.PageInfo;
import com.itqf.studentinfo.entity.Classes;
import com.itqf.studentinfo.entity.ResultInfo;
import com.itqf.studentinfo.entity.Student;
import com.itqf.studentinfo.service.ClassesService;
import com.itqf.studentinfo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: studentinfo
 * @description:
 * @author: Mr.徐
 * @create: 2019-12-05 10:28
 * <p>
 * 人有生老三千疾,唯有相思不可以丶
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;

    @RequestMapping("show")
    public PageInfo<Student> show(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue ="3" )int pageSize ){
     PageInfo<Student>pd=studentService.queryAll(currentPage,pageSize);
     return pd;
    }
    @RequestMapping("delete")
    public ResultInfo delete(Integer id){
        studentService.delete(id);
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        return info;
    }

    @RequestMapping("gettypes")
    public List<Classes> gettypes(ModelMap map){
        List<Classes> classesList =classesService.getAll();
        System.err.println(classesList);

        return classesList;

    }

    @RequestMapping("addstu")
    public ResultInfo add(Student student){
        int i = studentService.add(student);
        if (i>0){
            ResultInfo info=new ResultInfo();
            info.setFlag(true);
            return info;
        }
        return null;
    }
    @RequestMapping("find")
    public PageInfo<Student> find(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue ="3" )int pageSize,String name){
        PageInfo<Student> pd = studentService.find(currentPage, pageSize, name);
        System.err.println(pd);
        return pd;
    }

    @RequestMapping("findOne")
    public Student findOne( int id){
       Student student =studentService.findOne(id);
       return student;

    }

    @RequestMapping("updatestu")
    public ResultInfo updatestu(Student student){
        int i = studentService.updatestu(student);
        if (i>0){
            ResultInfo info=new ResultInfo();
            info.setFlag(true);
            return info;
        }
        return null;
    }

    @RequestMapping("/ttt")
    public String show(){
        return "Success";
    }

}
